<?php

require_once ROOT_SERVICES . DS . 'shipping_provider_interface.php';
require_once ROOT_LIBRARY_HTMLDOM . DS . 'simple_html_dom.php';
require_once ROOT_LIBRARY_CURL . DS . 'curl.php';
require_once ROOT_LIBRARY_CURL . DS . 'curl_response.php';

// id
class shipping_provider_2 implements shipping_provider_interface {

    private $_shipment;

    public function __construct($shipment) {
        $this->_shipment = $shipment;
    }

    public function get_status_shipping() {
        try
        {

            $tracking_number = $this->_shipment ['tracking_number'];
            $url = isset($this->_shipment ['url']) ? $this->_shipment ['url'] : "http://www.rpxholding.com/index.php?option=com_domestictracking";
            $url_post = 'http://www.rpxholding.com/?option=com_domestictracking&request=1';
            $html = file_get_html($url);
            $status_list = array();

            $params = array();
            $input = $html->find('input');
            foreach ($input as $i) {
                $params["{$i->name}"] = $i->value;
            }

            //pass captcha
            $input_math = $html->find('input[name=mathguard_answer]', 0);
            $str_math = $input_math->parent()->plaintext;
            $sta_math = preg_replace('/^[a-zA-Z\s:]*/', '', $str_math);
            list($n1, $op, $n2) = sscanf($sta_math, '%d%s%d=');

            $result = 0;
            switch ($op) {
                case '+': $result = $n1 + $n2;
                    break;
                case '-': $result = $n1 - $n2;
                    break;
                case '*': $result = $n1 * $n2;
                    break;
                case '/': $result = $n1 / $n2;
                    break;
            }

            $params['mathguard_answer'] = $result;
            $params['awbnum'] = $tracking_number;
            unset($params['searchword']);
            unset($params['option']);
            unset($params['Itemid']);
            $params['rdgTrackBy'] = 'awb';

            $curl = new Curl();
            $respone_res = $curl->post($url_post, $params);

            $html_res = new simple_html_dom();
            $html_res->load($respone_res->body);
            foreach ($html_res->find('table tbody tr') as $tr) {

                $tds = $tr->find('td');
                if (count($tds) == 6) {
                    $scan = array_shift($tds);
                    $activity = array_shift($tds);
                    array_shift($tds);
                    $location = array_shift($tds);
                    $date = array_shift($tds);
                    $time = array_shift($tds);

                    $check_time_m = preg_match("/(\d{2}):(\d{2}):(\d{2})/", $time->plaintext, $matches);
                    if ($check_time_m) {
                        $status_list[] = array(
                            'time' => $date->plaintext . ' ' . $time->plaintext,
                            'status' => "({$this->_shipment ['shipment_provider_name']}) " . preg_replace('!\s+!smi', '', strip_tags($activity->plaintext))
                        );
                    }
                }
            }
            return array('sortable' => TRUE, 'status_list' => $status_list);
        } catch (Exception $e)
        {
            return array('sortable' => FALSE, 'status_list' => null);
        }
    }

}

?>
